package com.shop.technicalservice.dao.user.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.shop.technicalservice.dao.user.UsersDao;
import com.shop.technicalservice.model.user.Users;

/**
 * A data access object (DAO) providing persistence and search support for Users
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see org.rjb.pojo.UsersDto
 * @author MyEclipse Persistence Tools
 */

public class UsersDaoImpl extends HibernateDaoSupport implements UsersDao{
	private static final Log log = LogFactory.getLog(UsersDaoImpl.class);
	// property constants
	public static final String NAME = "name";
	public static final String PWD = "pwd";
	public static final String QUESTION = "question";
	public static final String RESULT = "result";
	public static final String SAFTY_CODE = "saftyCode";

	protected void initDao() {
		// do nothing
	}

	public Users save(Users transientInstance) throws Exception {
		log.debug("saving Users instance");
			getHibernateTemplate().save(transientInstance);
			System.out.println("gggggggggggggggggg");
			log.debug("save successful");
		return transientInstance;
	}

	public void delete(Users persistentInstance) throws Exception{
		log.debug("deleting Users instance");
		 
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		 
	}

	public Users findById(java.lang.Integer id) throws Exception{
		log.debug("getting Users instance with id: " + id);
		 
			Users instance = (Users) getHibernateTemplate().get(
					"org.rjb.Users", id);
			return instance;
		 
	}

	public List findByExample(Users instance) throws Exception{
		log.debug("finding Users instance by example");
		 
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		 
	}

	public List findByProperty(String propertyName, Object value) throws Exception{
		log.debug("finding Users instance with property: " + propertyName
				+ ", value: " + value);
		 
			String queryString = "from Users as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		 
	}

	public List findByName(Object name) throws Exception{
		return findByProperty(NAME, name);
	}

	public List findByPwd(Object pwd) throws Exception{
		return findByProperty(PWD, pwd);
	}

	public List findByQuestion(Object question) throws Exception{
		return findByProperty(QUESTION, question);
	}

	public List findByResult(Object result) throws Exception{
		return findByProperty(RESULT, result);
	}

	public List findBySaftyCode(Object saftyCode) throws Exception{
		return findByProperty(SAFTY_CODE, saftyCode);
	}

	public List findAll() throws Exception{
		log.debug("finding all Users instances");
		 
			String queryString = "from Users";
			return getHibernateTemplate().find(queryString);
		 
	}

	public Users merge(Users detachedInstance) throws Exception{
		log.debug("merging Users instance");
		 
			Users result = (Users) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		 
	}

	public void attachDirty(Users instance) throws Exception{
		log.debug("attaching dirty Users instance");
		 
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		 
	}

	public void attachClean(Users instance) throws Exception{
		log.debug("attaching clean Users instance");
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
	}

	public static UsersDaoImpl getFromApplicationContext(ApplicationContext ctx) {
		return (UsersDaoImpl) ctx.getBean("UsersDAO");
	}
}